<!--老师查看活动列表-->
<link rel="stylesheet" href="./css/teacherActivities.css">

<!-- 添加活动模态框 -->
<div class="modal fade" id="tianjiahuodong" tabindex="-1" role="dialog" aria-labelledby="tianjiahuodong">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="tianjiahuodong1">添加活动</h4>
			</div>
			<div class="modal-body">
				<div class="input-group">
					<span class="input-group-addon" id="huodong_title">活动名称</span>
					<input id="hd_title" type="text" class="form-control" placeholder="请添加活动名称" aria-describedby="huodong_title">
				</div>
				<div class="input-group">
					<span class="input-group-addon" id="huodong_context">活动内容</span>
					<textarea id="hd_context" class="form-control" type="text" name="" placeholder="请添加活动内容" aria-describedby="huodong_context"></textarea>
				</div>
				<div class="input-group">
					<span class="input-group-addon" id="startTime">报名开始时间</span>
					<input type="text" class="demo-input" id="hd_start" placeholder="请选择日期" />
				</div>
				<div class="input-group">
					<span class="input-group-addon" id="endTime">报名结束时间</span>
					<input type="text" class="demo-input" id="hd_end" placeholder="请选择日期" />
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary huodong_add" data-dismiss="modal">确定添加</button>
			</div>
		</div>
	</div>
</div>

<!-- 修改活动模态框 -->
<div class="modal fade" id="xiugaihuodong" tabindex="-1" role="dialog" aria-labelledby="xiugaihuodong">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="xiugaihuodong1">修改活动</h4>
			</div>
			<div class="modal-body">
				<div class="input-group">
					<span class="input-group-addon" id="huodong_title">活动名称</span>
					<input id="xg_title" type="text" class="form-control" placeholder="请修改活动名称" aria-describedby="huodong_title">
				</div>
				<div class="input-group">
					<span class="input-group-addon" id="huodong_context">活动内容</span>
					<textarea id="xg_context" class="form-control" type="text" name="" placeholder="请修改活动内容" aria-describedby="huodong_context"></textarea>
				</div>
				<div class="input-group">
					<span class="input-group-addon" id="startTime">报名开始时间</span>
					<input type="text" class="demo-input" id="xg_start" placeholder="请选择日期" />
				</div>
				<div class="input-group">
					<span class="input-group-addon" id="endTime">报名结束时间</span>
					<input type="text" class="demo-input" id="xg_end" placeholder="请选择日期" />
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary huodong_xiugai" data-dismiss="modal">确定修改</button>
			</div>
		</div>
	</div>
</div>

<table class="table table-striped table-bordered table-hover turn" id="activity_on">
	<!--<thead>
		<tr>
			<td class="" style="width: 10%;"><b>活动标题</b></td>
			<td class="" style="width: 21%;"><b>活动内容</b></td>
			<td class="" style="width: 12%;"><b>发布时间</b></td>
			<td class="" style="width: 12%;"><b>报名开始时间</b></td>
			<td class="" style="width: 12%;"><b>报名截止时间</b></td>
			<td class="" style="width: 11%;"><b>操作</b></td>
		</tr>
	</thead>-->
</table>

<button style="float: right; margin-right: 12px;" type="button" class="btn btn-primary" data-toggle="modal" data-target="#tianjiahuodong">添加活动</button>

<nav aria-label="...">
	<ul class="pager">
		<li>
			<a href="#" id="pre">上一页</a>
		</li>
		<li>
			<a href="#" id="top">首页</a>
		</li>
		<li>第<span class="num">1</span>页</li>
		<li>
			<a href="#" id="end">尾页</a>
		</li>
		<li>
			<a href="#" id="next">下一页</a>
		</li>
	</ul>
</nav>

<script>
	//初始化日期编辑器
	laydate.render({
		elem: '#hd_start',
		type: 'datetime'
	});
	laydate.render({
		elem: '#hd_end',
		type: 'datetime'
	});
	laydate.render({
		elem: '#xg_start',
		type: 'datetime'
	});
	laydate.render({
		elem: '#xg_end',
		type: 'datetime'
	});

	//全局变量
	var pageSize = 15;
	var myajax;

	function fenye(pageNum) {
		myajax = $.ajax({ //初始化数据
			xhrFields: {
				withCredentials: true
			},
			type: "GET",
			dataType: 'json',
			url: getFullURL('all-activity-list'),
			data: {
				pageNum: pageNum,
				pageSize: pageSize,
				type: 1
			},
			success: function(data) {
				console.log(data);
				if(data.status == 0) {
					$.session.set("prePageNum", data.page.prePageNum);
					$.session.set("nextPageNum", data.page.nextPageNum);
					$.session.set("totalPageNum", data.page.totalPageNum);
					$('.num').html(data.page.pageNum);
					$('#activity_on').html("");
					var title = `
							<thead><tr>
								<td class="" style="width: 10%;"><b>活动标题</b></td>
								<td class="" style="width: 21%;"><b>活动内容</b></td>
								<td class="" style="width: 12%;"><b>发布时间</b></td>
								<td class="" style="width: 12%;"><b>报名开始时间</b></td>
								<td class="" style="width: 12%;"><b>报名截止时间</b></td>
								<td class="" style="width: 11%;"><b>操作</b></td>
							</tr></thead>
						`;
					$('#activity_on').append(title);

					var str_ = `<tbody>`;
					$.each(data.page.list, function(i, n) {
						var str = `
								<tr class="tr_shenhe" value="` + n.aid + `">
									<td>` + n.title + `</td>
									<td>` + n.context + `</td>
									<td>` + timeStamp2String(n.createTime) + `</td>
									<td>` + timeStamp2String(n.startTime) + `</td>
									<td>` + timeStamp2String(n.endTime) + `</td>
									<td>
										<button type="button" class="btn btn-primary btn-xs change" value="` + n.aid + `">修改</button>
										<button type="button" class="btn btn-primary btn-xs delete" value="` + n.aid + `">删除</button>
										<button type="button" class="btn btn-primary btn-xs shenhe" value="` + n.aid + `">审核</button>
									</td>
								</tr>
							`;
						str_ = str_ + str;
					})
					str_ = str_ + `</tbody>`;
					$('#activity_on').append(str_);

					$('.shenhe').click(function() { //审核活动
						$.session.set("aid", $(this).val());

						$('.center').html('');
						$('.center').load('./teacherActivity.html');
						return false;
					})

					$('.tr_shenhe').click(function() { //审核活动
						$.session.set("aid", $(this).attr("value"));

						$('.center').html('');
						$('.center').load('./teacherActivity.html');
						return false;
					})
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	}

	fenye(1);

	$("#top").click(function() {
		fenye(1);
	})
	$("#end").click(function() {
		fenye($.session.get("totalPageNum"));
	})
	$("#pre").click(function() {
		fenye($.session.get("prePageNum"));
	})
	$("#next").click(function() {
		fenye($.session.get("nextPageNum"));
	})

	$.when(myajax).done(function() {
		$('.change').click(function() { //修改活动
			$.session.set("aid", $(this).val());

			$.ajax({ //初始化数据
				xhrFields: {
					withCredentials: true
				},
				type: "GET",
				dataType: 'json',
				url: getFullURL('get-one-a'),
				data: {
					aid: $.session.get("aid")
				},
				success: function(data) {
					console.log(data)
					if(data.status == 0) {
						$("#xg_title").val(data.activity.title),
							$("#xg_context").val(data.activity.context),
							$("#xg_start").val(timeStamp2String(data.activity.startTime)),
							$("#xg_end").val(timeStamp2String(data.activity.endTime))

						$(xiugaihuodong).modal({
							keyboard: true,
							show: true
						})
					}
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
			return false;
		})

		$('.delete').click(function() { //删除活动
			$.ajax({
				xhrFields: {
					withCredentials: true
				},
				type: "GET",
				dataType: 'json',
				url: getFullURL('teacher/del-activity'),
				data: {
					aid: $(this).val()
				},
				success: function(data) {
					if(data.status == 0) {
						alert('删除成功')
						setTimeout(function() {
							$('.center').html('');
							$('.center').load('./teacherActivities.html');
						}, 300)
					}
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
			return false;
		})
	})

	$(".huodong_add").click(function() { //添加活动
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "post",
			dataType: 'json',
			url: getFullURL('teacher/publish'),
			data: {
				title: $("#hd_title").val(),
				context: $("#hd_context").val(),
				startTime: $("#hd_start").val(),
				endTime: $("#hd_end").val()
			},
			success: function(data) {
				if(data.status == 0) {
					alert('发布成功')
					setTimeout(function() {
						$('.center').html('');
						$('.center').load('./teacherActivities.html');
					}, 300)
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	$(".huodong_xiugai").click(function() { //修改活动
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "post",
			dataType: 'json',
			url: getFullURL('teacher/update-activity'),
			data: {
				aid: $.session.get('aid'),
				title: $("#xg_title").val(),
				context: $("#xg_context").val(),
				startTime: $("#xg_start").val(),
				endTime: $("#xg_end").val()
			},
			success: function(data) {
				if(data.status == 0) {
					alert('修改成功')
					setTimeout(function() {
						$('.center').html('');
						$('.center').load('./teacherActivities.html');
					}, 300)
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	function timeStamp2String(time) {

		if(time == null || time == "") {
			return "时间未设置"
		}

		var datetime = new Date();
		datetime.setTime(time);
		var year = datetime.getFullYear();
		var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
	}
</script>